#!/usr/bin/env bash

set -Eeuxo pipefail

pushd "$(dirname "$0")" >/dev/null

target="${TARGET:-bundler}"

rm -rf pkg
wasm-pack build --target "$target"
pushd pkg
# `bundler` is for WASM/npm, `deno` is for Deno.
if [[ "$target" = "bundler" ]]; then
  jq -s '.[0] * .[1]' package.json ../package.merge.json > package.new.json
  mv package.new.json package.json
  rm .gitignore
elif [[ "$target" = "deno" ]]; then
  rm .gitignore
  cp ../deno.json .
  # Necessary for Deno.
  sed -i '1s%^%/* @ts-self-types="./index.d.ts" */\n%' index.js
fi
popd

popd >/dev/null
